package com.diankangwo.factory.controller;

import cn.beecloud.BCEumeration;
import cn.beecloud.bean.BCRefund;
import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.PageUtils;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.factory.dao.TdMessageMapper;
import com.diankangwo.factory.entity.TdMessage;
import com.diankangwo.factory.entity.TdOrder;
import com.diankangwo.factory.entity.TdRefundMoney;
import com.diankangwo.factory.entity.TdUserinfo;
import com.diankangwo.factory.service.TdOrderService;
import com.diankangwo.factory.service.TdRefundMoneyService;
import com.diankangwo.factory.service.TdUserInfoService;
import com.diankangwo.util.BeecloudPayUtil;
import com.diankangwo.util.MessageType;
import com.diankangwo.util.PayStatus;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 退款Controller
 */
@RestController
@RequestMapping("/fac/refundMoney")
public class TdRefundMoneyController {

    @Autowired
    private TdRefundMoneyService tdRefundMoneyService;
    @Autowired
    private TdOrderService tdOrderService;
    @Autowired
    private TdMessageMapper tdMessageMapper;
    @Autowired
    private TdUserInfoService tdUserInfoService;

    /**
     * 获取退款列表
     */
    @ApiOperation(value = "获取退款列表", notes = "")
    @ApiImplicitParam(name = "params", value = "查询条件", required = true, dataType = "Map<String,Object>")
    @RequestMapping("/list")
    @RequiresPermissions("fac:refundMoney:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TdRefundMoney> tdRefundMonies = tdRefundMoneyService.queryList(query);
        //为DTO赋值，并查询出用户信息参数
        int total = tdRefundMoneyService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tdRefundMonies, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改", notes = "")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @SysLog("修改商品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @RequiresPermissions("fac:refundMoney:update")
    public R update(int id, String type, String rejectfundcause) {
        TdRefundMoney tdRefundMoney = tdRefundMoneyService.selectByPrimaryKey(id);
        String url = "";
        if (type.equals("通过")) {
            tdRefundMoney.setState(1);
            TdOrder tdOrder = tdOrderService.queryByOrderNo(tdRefundMoney.getPaymenorderno());

            //执行退款操作
            BeecloudPayUtil util = new BeecloudPayUtil();
            Map<String, Object> map = new HashMap<>();
            map.put("orderNo", tdRefundMoney.getPaymenorderno());
            BCRefund bcRefund = new BCRefund();
            if (tdOrder.getPaytype().equals("ALI_APP")) {
                bcRefund = util.BCRefundParamter(tdRefundMoney.getPaymenorderno(), tdRefundMoney.getRefundno(), tdRefundMoney.getMoney(), BCEumeration.PAY_CHANNEL.ALI, map);
            } else {
                tdOrder.setOrderStatus(PayStatus.refundsuccess.getName());
                tdRefundMoney.setState(3);
                tdRefundMoneyService.updateByPrimaryKey(tdRefundMoney);
                tdOrderService.updateByPrimaryKey(tdOrder);
                bcRefund = util.BCRefundParamter(tdRefundMoney.getPaymenorderno(), tdRefundMoney.getRefundno(), tdRefundMoney.getMoney(), BCEumeration.PAY_CHANNEL.WX, map);
            }
            if (bcRefund.getAliRefundUrl() != null) {
                url = bcRefund.getAliRefundUrl();
            } else {
                System.out.println("退款成功！易宝、百度、快钱渠道还需要定期查询退款结果！");
                System.out.println(bcRefund.getObjectId());
            }
            if (bcRefund.isRefunded()) {
                //改变订单信息
                tdOrder.setOrderStatus(PayStatus.refund.getName());
                tdOrderService.updateByPrimaryKey(tdOrder);
                //记录消息
                TdMessage tdMessage = new TdMessage("您的订单：" + tdOrder.getOrderNo() + "已同意退款申请！正在打款！", 1, new Date(), MessageType.order_refund.getName(), tdOrder.getUid(), "");
                tdMessageMapper.insert(tdMessage);
            }
        } else {
            tdRefundMoney.setRejectfundcause(rejectfundcause);
            tdRefundMoney.setState(2);
            //改变订单信息
            TdOrder tdOrder = tdOrderService.queryByOrderNo(tdRefundMoney.getPaymenorderno());
            tdOrder.setOrderStatus(PayStatus.rejectfund.getName());
            tdOrderService.updateByPrimaryKey(tdOrder);
            //记录消息
            TdUserinfo tdUserinfo = tdUserInfoService.selectByUid(tdOrder.getUid());
            TdMessage tdMessage = new TdMessage("您的订单：" + tdOrder.getOrderNo() + "退款申请已被拒绝！拒绝原因：" + tdRefundMoney.getRejectfundcause(), 1, new Date(), MessageType.order_refundsuccess.getName(), tdOrder.getUid(), tdUserinfo.getGetuiCid());
            tdMessageMapper.insert(tdMessage);
        }
        tdRefundMoneyService.updateByPrimaryKey(tdRefundMoney);
        return R.ok().put("url", url);
    }


}
